//
//  SkyAssociationMenuView.m
//
//  Created by skytoup on 14-10-24.
//  Copyright (c) 2014年 skytoup. All rights reserved.
//

#import "SkyAssociationMenuView.h"
#import "QuestionTypeTableViewCell.h"

NSString *const IDENTIFIER = @"QuestionTypeTableViewCell";

@interface SkyAssociationMenuView () <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>
{
    NSArray *tables;

}
@property(nonatomic,strong)UIView *baView;
@property(nonatomic,strong)NSMutableDictionary * choseIndexPath;
@end

@implementation SkyAssociationMenuView

- (instancetype)initWithFrame:(CGRect)frame ;
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化选择项
        for(int i=0; i!=3; ++i) {
            sels[i] = -1;
        }
        _choseIndexPath = [NSMutableDictionary dictionary];
        self.frame = frame;
        self.userInteractionEnabled = YES;

    }
    return self;
}

#pragma mark private
/**
 *  调整表视图的位置、大小
 */
- (void)adjustTableViews{

        UITableView *t0 = [tables objectAtIndex:0];
        UITableView *t1 = [tables objectAtIndex:1];
        UITableView *t2 = [tables objectAtIndex:2];
        NSIndexPath * path0 = _choseIndexPath[@"0"];
        NSIndexPath * path1 = _choseIndexPath[@"1"];
        NSIndexPath * path2 = _choseIndexPath[@"2"];
    
    if (path0 && path1 && path2) {
        t0.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 46*VerticalRatio());
        [t0 scrollToRowAtIndexPath:path0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t0.hidden = NO;
        
        t1.frame = CGRectMake(0, 46*VerticalRatio(), CGRectGetWidth(self.frame), 46*VerticalRatio());
        [t1 scrollToRowAtIndexPath:path1 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t1.hidden = NO;
        
         t2.frame = CGRectMake(0, 92*VerticalRatio(), CGRectGetWidth(self.frame),CGRectGetHeight(self.frame)-92*VerticalRatio());
        [t2 scrollToRowAtIndexPath:path2 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t2.hidden = NO;
    }else if (path0 && path1 && !path2){
        t0.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 46*VerticalRatio());

        [t0 scrollToRowAtIndexPath:path0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t0.hidden = NO;
        
        t1.frame = CGRectMake(0, 46*VerticalRatio(), CGRectGetWidth(self.frame),CGRectGetHeight(self.frame)-46*VerticalRatio());
        if (t2.hidden == NO) {
            t1.frame = CGRectMake(0, 46*VerticalRatio(), CGRectGetWidth(self.frame), 46*VerticalRatio());
        }
        [t1 scrollToRowAtIndexPath:path1 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t1.hidden = NO;
    }else if (path0 && !path1 &&!path2){
       
        t0.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        if (t1.hidden == NO) {
            t0.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 46*VerticalRatio());
        }
        [t0 scrollToRowAtIndexPath:path0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t0.hidden = NO;
    }else if (!path0 && !path1 &&!path2){
        t0.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        [t0 scrollToRowAtIndexPath:path0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        t0.hidden = NO;
    }
    
    
    
   
    
}


/**
 *  加载保存的选中项
 */
- (void)loadSels{
    
    NSIndexPath * path0 = _choseIndexPath[@"0"];
    NSIndexPath * path1 = _choseIndexPath[@"1"];
    NSIndexPath * path2 = _choseIndexPath[@"2"];
    UITableView *t0 = [tables objectAtIndex:0];
    UITableView *t1 = [tables objectAtIndex:1];
    UITableView *t2 = [tables objectAtIndex:2];
    
    if (path0) {
        QuestionTypeTableViewCell * cell = [t0 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:path0.row inSection:0]];
        cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
        cell.typeButton.selected = YES;
    }
    
    if (path1) {
        QuestionTypeTableViewCell * cell = [t1 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:path1.row inSection:0]];
        cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
        cell.typeButton.selected = YES;
    }
    
    if (path2) {
        QuestionTypeTableViewCell * cell = [t2 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:path2.row inSection:0]];
        cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
        cell.typeButton.selected = YES;
    }
    
}

#pragma mark public
- (void)setSelectIndexForClass1:(NSInteger)idx_1 class2:(NSInteger)idx_2 class3:(NSInteger)idx_3 {
    sels[0] = idx_1;
    sels[1] = idx_2;
    sels[2] = idx_3;
}

- (void)showAsDrawDownView{
  
    if(!_baView.superview) {
        // 初始化菜单
        tables = @[[[UITableView alloc] init], [[UITableView alloc] init], [[UITableView alloc] init] ];
        [tables enumerateObjectsUsingBlock:^(UITableView *table, NSUInteger idx, BOOL *stop) {
            table.frame = CGRectMake(0, 46*idx*VerticalRatio(), self.frame.size.width, self.frame.size.height-46*idx*VerticalRatio());
            table.dataSource = self;
            table.delegate = self;
            table.separatorStyle = UITableViewCellSeparatorStyleNone;
            [table registerNib:[UINib nibWithNibName:IDENTIFIER bundle:nil] forCellReuseIdentifier:IDENTIFIER];
            table.backgroundColor = [UIColor whiteColor];
            if (idx>0) {
                table.hidden = YES;
            }
        }];

        
        _baView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        _baView.userInteractionEnabled = YES;
        _baView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baView];
    }
    [_baView addSubview:[tables objectAtIndex:0]];
     [_baView addSubview:[tables objectAtIndex:1]];
     [_baView addSubview:[tables objectAtIndex:2]];
    
    [self adjustTableViews];
    [self loadSels];
    
    if(!self.superview) {
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        self.alpha = .0f;
        [UIView animateWithDuration:.25f animations:^{
            self.alpha = 1.0f;
        }];
    }
    [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:self];
}

- (void)dismiss{
    if(self.superview) {
        [UIView animateWithDuration:.25f animations:^{
            self.alpha = .0f;
        } completion:^(BOOL finished) {
            [_baView.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
                [obj removeFromSuperview];
            }];
            [_baView removeFromSuperview];
            [self removeFromSuperview];
        }];
    }
}

#pragma mark UITableViewDateSourceDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    cell.typeName.textColor = [UIColor colorWithWhite:0.196 alpha:1.000];
    cell.typeButton.selected = NO;
    if(tableView == [tables objectAtIndex:0]){
        cell.typeName.text = [_delegate assciationMenuView:self titleForClass_1:indexPath.row];
        if (indexPath == _choseIndexPath[@"0"]) {
            cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
            cell.typeButton.selected = YES;
        }
    }else if(tableView == [tables objectAtIndex:1]){
         cell.typeName.text =[_delegate assciationMenuView:self titleForClass_1:((UITableView*)tables[0]).indexPathForSelectedRow.row class_2:indexPath.row];
        if (indexPath == _choseIndexPath[@"1"]) {
            cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
            cell.typeButton.selected = YES;
        }
    }else if(tableView == [tables objectAtIndex:2]){
        cell.typeName.text = [_delegate assciationMenuView:self titleForClass_1:((UITableView*)tables[0]).indexPathForSelectedRow.row class_2:((UITableView*)tables[1]).indexPathForSelectedRow.row class_3:indexPath.row];
        if (indexPath == _choseIndexPath[@"2"]) {
            cell.typeName.textColor = [UIColor colorWithRed:1.000 green:0.565 blue:0.047 alpha:1.000];
            cell.typeButton.selected = YES;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 46*VerticalRatio();
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger __block count;
    [tables enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if(obj == tableView) {
            count = [_delegate assciationMenuView:self countForClass:idx];
            *stop = YES;
        }
    }];
    return count;
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableView *t0 = [tables objectAtIndex:0];
    UITableView *t1 = [tables objectAtIndex:1];
    UITableView *t2 = [tables objectAtIndex:2];
    if(tableView == t0){
        [_choseIndexPath setObject:indexPath forKey:@"0"];
        if([self.delegate respondsToSelector:@selector(assciationMenuView:idxChooseInClass1:)]) {
             [_delegate assciationMenuView:self idxChooseInClass1:indexPath.row];
        }

            if(t1.hidden == YES) {
                t1.hidden = NO;
                [t1 reloadData];
            }else{
                t1.hidden = YES;
                [_choseIndexPath removeObjectForKey:@"1"];
            }
            
            if(t2.hidden == NO) {
                t2.hidden = YES;
                [_choseIndexPath removeObjectForKey:@"2"];
            }

    }else if(tableView == t1) {
        [_choseIndexPath setObject:indexPath forKey:@"1"];
        if([self.delegate respondsToSelector:@selector(assciationMenuView:idxChooseInClass1:class2:)]) {
             [_delegate assciationMenuView:self idxChooseInClass1:t0.indexPathForSelectedRow.row class2:indexPath.row];
        }

            if(t2.hidden == YES) {

               t2.hidden = NO;
                [t2 reloadData];
            }else{
                t2.hidden = YES;
                [_choseIndexPath removeObjectForKey:@"2"];
            }
        
    }else if(tableView == t2) {
        [_choseIndexPath setObject:indexPath forKey:@"2"];
        if([self.delegate respondsToSelector:@selector(assciationMenuView:idxChooseInClass1:class2:class3:)]) {
        [_delegate assciationMenuView:self idxChooseInClass1:t0.indexPathForSelectedRow.row class2:t1.indexPathForSelectedRow.row class3:indexPath.row];
        }

        [self dismiss];

    }
    [self adjustTableViews];
    [self loadSels];
}

@end
